package com.incar.chuxing.baiduMap;

import android.content.Context;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Michael on 10/31.
 */

public class MapStatusUtils {
    private BaiduMap mBaiduMap;

    public MapStatusUtils(BaiduMap mBaiduMap) {
        this.mBaiduMap = mBaiduMap;
    }

    public MapStatusUtils() {
    }

    /**
     * 平滑移动地图的中心和缩放
     *
     * @param targetPosition 地图中心
     * @param zoom           缩放范围 3~21
     * @param duration       平滑移动的时间 毫秒值
     */
    public void animateMove(LatLng targetPosition, float zoom, int duration) {
        if (duration == 0) {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(targetPosition, zoom));
        } else {
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(targetPosition, zoom), duration);
        }
    }

    public void defaultPosition() {
        MapStatus.Builder builder = new MapStatus.Builder();
        LatLng center = new LatLng(39.915071, 116.403907); // 默认 天安门
        builder.target(center).zoom(18);
    }


    public void clearMap() {
        mBaiduMap.clear();
    }

    // 设置个性化地图config文件路径
    public void setMapCustomFile(Context context, String PATH) {
        FileOutputStream out = null;
        InputStream inputStream = null;
        String moduleName = null;
        try {
            inputStream = context.getAssets().open("customConfigdir/" + PATH);
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);

            moduleName = context.getFilesDir().getAbsolutePath();
            File f = new File(moduleName + "/" + PATH);
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
            out = new FileOutputStream(f);
            out.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MapView.setCustomMapStylePath(moduleName + "/" + PATH);
    }
}
